package cn.xtits.xtf.web.springmvc;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Enumeration;

public class RequestLogInterceptor extends HandlerInterceptorAdapter {

    public static final Logger logger = LoggerFactory.getLogger(RequestLogInterceptor.class);


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        request.setAttribute("startTime", new Date().getTime());

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        Long startTime = (Long) request.getAttribute("startTime");
        logger.info("url¤{}¤{}¤{}¤ ", new Object[]{request.getRequestURI(), (new Date().getTime() - startTime), getStringRequestParam(request)});
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }

    private String getStringRequestParam(HttpServletRequest request) {
        Enumeration enumeration = request.getParameterNames();
        StringBuffer dataSb = new StringBuffer();
        while (enumeration.hasMoreElements()) {
            String key = (String) enumeration.nextElement();
            String data = request.getParameter(key);

            dataSb.append(key).append("=").append(data).append("&");
        }
        if (dataSb.length() > 0) {
            dataSb.deleteCharAt(dataSb.length() - 1);
        }
        String s = dataSb.toString();
        if (s.equals("")) {
            return " ";
        } else {
            return s;
        }
    }
}
